<?php
declare (strict_types = 1);

namespace app\admin\service\user;

use app\admin\logic\user\Group as UserGroupLogic;
use app\admin\logic\user\Permission as PermissionLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\Request;

/**
 * 角色接口类
 */
class Group extends BaseService
{
    /**
     * 用户角色列表首页
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-19
     * @return   [type]                         [description]
     */
    public function index()
    {
        $data = app(UserGroupLogic::class)->getList();
        // 定义字段
        $keyList = [
            'title'       => ListItem::make()->title('名称'),
            'update_time' => ListItem::make()->title('更新时间'),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 获取所有角色
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-19
     */
    public function groups()
    {
        $groups = app(UserGroupLogic::class)->getGroupsWithout2();
        return $this->parseToData($groups->toArray(), 1, '获取成功');
    }

    /**
     * 创建角色
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-17
     */
    public function create(Request $request)
    {
        $data = $request->post();
        $user = app(UserGroupLogic::class)->createGroup($data);

        return $this->parseToData($user->toArray(), 1, '创建成功');
    }

    /**
     * 编辑角色
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-17
     */
    public function edit(Request $request)
    {
        $data = $request->put();
        $data = app(UserGroupLogic::class)->editGroup($data);

        return $this->parseToData($data, 1, '编辑成功');
    }

    /**
     * 删除角色
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-17
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        app(UserGroupLogic::class)->deleteGroup($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 编辑用户组权限
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-23
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function permission(int $id, Request $request)
    {
        if ($request->isPut()) {
            $auths = [
                $request->put('checked/a', []),
                $request->put('half/a', []),
            ];

            app(UserGroupLogic::class)->setAuths($id, $auths);

            return $this->parseToData([], 1, '设置成功');
        }

        // 获取当前角色授权的节点
        $group = app(UserGroupLogic::class)->where('id', $id)->findOrFail();
        $auths = $group->rule_list;
        // 获取所有树形结构
        $treeList = app(PermissionLogic::class)->getTreeList();

        return $this->parseToData(['nodes' => $treeList, 'auths' => $auths], 1);

    }
}
